<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ClassHourList;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class ClassHourListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ClassHourList(), function (Grid $grid) {
//            $grid->column('id')->sortable();
            $grid->column('class_name','课时标题');

            $grid->column('senecd')->display(function ($value){
                return $value.'课时';
            });
            $grid->column('class_start_time')->display(function($value){
                  return date('Y-m-d H:i:s',$value);
            });
            $grid->column('class_end_time')->display(function($value){
                return date('Y-m-d H:i:s',$value);
            });
            $grid->column('course_id','所属课程')->display(function ($value){
                return  DB::table('course')->where('id',$value)->value('class_name');
            });
//            $grid->column('created_at')->display(function($value){
//                return date('Y-m-d H:i:s',$value);
//            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ClassHourList(), function (Show $show) {
            $show->field('id');
            $show->field('senecd');
            $show->field('class_name');
            $show->field('class_start_time');
            $show->field('class_end_time');
            $show->field('create_time');
            $show->field('course_id');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ClassHourList(), function (Form $form) {
            $form->display('id');
            $form->text('class_name');
            $form->number('senecd');
            $form->datetime('class_start_time');
            $form->datetime('class_end_time');
            $form->select('course_id','课程')->options('course/getCourse');

            $form->saving(function (Form $form){
                $form->class_start_time = strtotime($form->class_start_time);
                $form->class_end_time =strtotime($form->class_end_time);
            });
        });
    }
}
